<?php
global $wpdb;
global $table_ranges;
if($_REQUEST['action'] == 'addrange')
{
	$query = $wpdb->prepare("insert into $table_ranges ($table_ranges.name, $table_ranges.address, $table_ranges.phone) values (%s, %s, %s)",
							$_REQUEST['ranges_name'],
							$_REQUEST['ranges_address'],
							$_REQUEST['ranges_phone']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error() <br />\n";
	}
}
else if($_REQUEST['action'] == 'changerange')
{
	$query = $wpdb->prepare("update $table_ranges set " .
							"$table_ranges.name=%s, " . 
							"$table_ranges.address=%s, " .
							"$table_ranges.phone=%s " .
							"where $table_ranges.rangeid=%s",
							$_REQUEST['ranges_name'],
							$_REQUEST['ranges_address'],
							$_REQUEST['ranges_phone'],
							$_REQUEST['ranges_rangeid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error() <br />\n";
	}
}
else if($_REQUEST['action'] == 'remove')
{
	$query = $wpdb->prepare("delete from $table_ranges where rangeid=%s",
							$_REQUEST['ranges_rangeid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "ERROR: $wpdb->print_error() <br />\n";
	}
}
else if($_REQUEST['action'] == 'edit')
{
	global $table_ranges;
	$query = $wpdb->prepare("select $table_ranges.rangeid,$table_ranges.name,$table_ranges.address,$table_ranges.phone " .
							   "from $table_ranges " . 
							   "where $table_ranges.rangeid=%d",
							   $_REQUEST['ranges_rangeid']);
	$rangedata = $wpdb->get_row($query);
?>
<form action="admin.php?page=<?php print $GLOBALS['ts_path'] ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Edit Range:</h2>
<div>
<label for="name">Name:</label>
<input type="text" name="ranges_name" id="name" value="<?php echo $rangedata->name ?>" size="64" /><br />
<label for="address">Address:</label>
<input type="text" name="ranges_address" id="address" value="<?php echo $rangedata->address ?>" size="64" /><br />
<label for="phone">Phone Number:</label>
<input type="text" name="ranges_phone" id="phone" value="<?php echo $rangedata->phone ?>" size="20" /><br />
<input type="hidden" name="ranges_rangeid" value="<?php echo $rangedata->rangeid ?>" />
<input type="hidden" name="action" value="changerange" />
</div>
<div class="submit"><input type="submit" value="Update Range &raquo;" /></div>
</form>
<?php
return;
}
?>

<form action="admin.php?page=<?php print $GLOBALS['ts_path']; ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Current Ranges</h2>
<table>
<th width=20%>Name</th><th width=20%>Address</th><th width=20%>Phone Number</th><th width=10%>Fields</th><th colspan="3">Actions</th>
<?php
global $table_ranges;
$ranges = $wpdb->get_results("select $table_ranges.rangeid,$table_ranges.name,$table_ranges.address,$table_ranges.phone from $table_ranges");
if ($ranges === FALSE) 
{
    die($wpdb->print_error());
}
foreach($ranges as $range)
{
    echo "<tr>\n";
    echo "<td>$range->name</td><td>$range->address</td><td>$range->phone</td><td>fields</td>\n";
    echo "<td><a href=\"admin.php?page=". $GLOBALS['ts_path'] . "/" . basename(__FILE__) . "&amp;ranges_rangeid=" . $range->rangeid . "&amp;action=edit\" class=\"edit\">Edit</a></td>";
    echo "<td><a href=\"admin.php?page=". $GLOBALS['ts_path'] . "/" . basename(__FILE__) . "&amp;ranges_rangeid=" . $range->rangeid . "&amp;action=remove\" class=\"delete\">Remove</a></td>";
    echo "<td><input type=\"checkbox\" name=\"shooters_shooterid[]\" value=\"" . $row['shooterid']. "\"></td>\n";
    echo "</tr>\n";
}
?>
</table>
<div class="submit">
<input type="submit" class="delete" name="action" value="Remove Selected Ranges" />
</div>
</div>
</form>

<form action="admin.php?page=<?php print $GLOBALS['ts_path'] ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Add a New Range:</h2>
<div>
<label for="name">Name:</label>
<input type="text" name="ranges_name" id="name" value="name" size="64" /><br />
<label for="address">Address:</label>
<input type="text" name="ranges_address" id="address" value="address" size="64" /><br />
<label for="phone">Phone Number:</label>
<input type="text" name="ranges_phone" id="phone" value="1234567890" size="20" /><br />
<input type="hidden" name="action" value="addrange" />
</div>
<div class="submit"><input type="submit" value="Add Range &raquo;" /></div>
</form>
</div>
